﻿using MyChannel.Library;
using MyChannel.Resources;
using MyChannel.SupportListview;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace MyChannel.DetailCategory
{
    class ListDetailCategory
    {
        MD5 md5 = new MD5();
        public ListDetailCategory()
        {

            Error = false;
            Busy = true;
            AddMoreItems();

            fetchMoreDataCommand = new DelegateCommand(
                obj =>
                {
                    return;
                    if (busy)
                    {
                        return;
                    }
                    Busy = true;
                    ThreadPool.QueueUserWorkItem(
                        delegate
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(
                                delegate
                                {
                                    AddMoreItems();
                                });
                        });

                });
        }


        void AddMoreItems()
        {
            int page = 0;
            int size = 10;
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
            string timetamp = md5.GetTime();
            webclient.DownloadStringAsync(new Uri(AppResources.Server + string.Format(AppResources.GetDetailCategory, App.CategoryAction, page.ToString(), size.ToString(), App.OsName, App.OsVersion, App.DeviceID, md5.GetMD5(AppResources.MD5KEY + timetamp), timetamp, App.DeviceName)));
        }
        void webclient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
                System.Diagnostics.Debug.WriteLine(json);
                if (!string.IsNullOrEmpty(json))
                {
                    RootDetailCategoryObject rootDetailCategoryObject = JsonConvert.DeserializeObject<RootDetailCategoryObject>(json);
                    for (int i=0; i < rootDetailCategoryObject.Data.Count; i++)
                    {
                        items.Add(rootDetailCategoryObject.Data[i]);
                    }
                    Busy = false;
                }
            }
            catch (Exception ex)
            {
                Error = true;
                Busy = false;

            }


        }

        readonly DelegateCommand fetchMoreDataCommand;

        public ICommand FetchMoreDataCommand
        {
            get
            {
                return fetchMoreDataCommand;
            }
        }

        readonly ObservableCollection<DetailCategoryObject> items = new ObservableCollection<DetailCategoryObject>();

        public ObservableCollection<DetailCategoryObject> ItemsOfDetailCategory
        {
            get
            {
                return items;
            }
        }

        bool busy;

        public bool Busy
        {
            get
            {
                return busy;
            }
            set
            {
                if (busy == value)
                {
                    return;
                }
                busy = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Busy"));
            }
        }
        bool error;
        public bool Error
        {
            get
            {
                return error;
            }
            set
            {
                if (error == value)
                {
                    return;
                }
                error = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Error"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var tempEvent = PropertyChanged;
            if (tempEvent != null)
            {
                tempEvent(this, e);
            }
        }
    }
}
